package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupDelRelVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author tianfengbo
 * @email tianfengbo@gmail.com
 * @date 2021-06-10 16:39:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;



    @GetMapping("/{catelogId}/withattr")
    private R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrs> attrs=attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",attrs);
    }
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupDelRelVo> attrGroupDelRelVos){
        attrGroupService.addRelation(attrGroupDelRelVos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelation(@RequestParam Map<String, Object> params,
                           @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page=attrService.getNoRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /*
    * 删除属性分组的关联
    * */
    @PostMapping("/attr/relation/delete")
    public R deleteRels(@RequestBody List<AttrGroupDelRelVo> attrGroupDelRelVos){
        attrAttrgroupRelationService.deleteRels(attrGroupDelRelVos);
        return R.ok();
    }
    /*
    * 获取属性分组的关联列表
    * */
@GetMapping("/{attrgroupId}/attr/relation")
    public R getRelationList(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list=attrGroupService.getRelationList(attrgroupId);
    return R.ok().put("data",list);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R listByCatelogId(@RequestParam Map<String, Object> params,
                             @PathVariable("catelogId")Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page=attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //根据catelogId找到catelogpaths
        Long[] path=categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
